package com.mingqijia.gassafety.console.controller;

import com.gap.basic.base.CommonResponse;
import com.mingqijia.gassafety.shared.constant.OperateRecordEnum;
import com.mingqijia.gassafety.shared.utils.HeaderUtil;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.webserver.request.*;
import com.mingqijia.gassafety.webserver.response.LocalMessageResponse;
import com.mingqijia.gassafety.webserver.response.StaffListResponse;
import com.mingqijia.gassafety.webserver.response.WarnStrategyRes;
import com.mingqijia.gassafety.webserver.response.WarnStrategyResponse;
import com.mingqijia.gassafety.webserver.service.WarnStrategyService;
import com.mingqijia.gassafety.shared.log.LogRecord;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author zhangfei
 * @version 1.0.0
 * @ClassName WarnStrategyController.java
 * @createTime 2022年02月25日
 */
@RestController
@Api(tags = "报警策略")
@RequestMapping("/business/v1/warnStrategy")
@Slf4j
public class WarnStrategyController {

    @Autowired
    private WarnStrategyService warnStrategyService;

    @Autowired
    HeaderUtil headerUtil;

    /**
     * 新增报警策略
     *
     * @param request
     * @return
     */
    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_23,commonInfo = "#request.ruleName" )
    @PostMapping("saveWarnStrategy")
    @ApiOperation(value = "新增报警策略")
    public CommonResponse<Object> saveWarnStrategy(@RequestBody @Valid SaveWarnStrategyRequest request) {
        return CommonResponse.success(warnStrategyService.saveWarnStrategy(request,request.getSource()));
    }

    /**
     * 编辑报警策略
     *
     * @param request
     * @return
     */
    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_24,commonInfo ="#request.ruleName" ,detail = "'【规则名称：' + #request.ruleName + '】\n'")
    @PostMapping("editWarnStrategy")
    @ApiOperation(value = "编辑报警策略")
    public CommonResponse<Object> editWarnStrategy(@RequestBody @Valid SaveWarnStrategyRequest request) {
        return CommonResponse.success(warnStrategyService.editWarnStrategy(request,request.getSource()));
    }

    /**
     * 查询报警策略
     *
     * @param
     * @return
     */
    @GetMapping("warnStrategyInfo")
    @ApiOperation(value = "查询报警策略")
    public CommonResponse<WarnStrategyResponse> warnStrategyInfo(@RequestParam("id") Long id) {
        return CommonResponse.success(warnStrategyService.warnStrategyInfo(id));
    }

    /**
     * 查询报警策略列表
     *
     * @param
     * @return
     */
    @PostMapping("warnStrategyList")
    @ApiOperation(value = "查询报警策略列表")
    public CommonResponse<PageUtil<WarnStrategyRes>> warnStrategyList(@RequestBody StrategyListRequest request) {
        request.setTemplateType(headerUtil.getTemplateType());
        return CommonResponse.success(warnStrategyService.warnStrategyList(request));
    }

    /**
     * 报警策略启用禁用
     *
     * @param
     * @return
     */
    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_25)
    @PostMapping("updateStatus")
    @ApiOperation(value = "报警策略启用禁用")
    public CommonResponse<Object> updateStatus(@RequestBody @Valid UpdateStrategyRequest request) {
        warnStrategyService.updateStatus(request);
        return CommonResponse.success(null);
    }

    /**
     * 报警策略删除
     *
     * @param
     * @return
     */
    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_26)
    @PostMapping("deleteWarnStrategy")
    @ApiOperation(value = "报警策略删除")
    public CommonResponse<Object> deleteWarnStrategy(@RequestBody @Valid DeleteWarnStrategyRequest request) {
        warnStrategyService.deleteWarnStrategy(request);
        return CommonResponse.success(null);
    }


    /**
     * 报警暂停
     *
     * @param
     * @return
     */
    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_33)
    @PostMapping("killWarning")
    @ApiOperation(value = "报警暂停")
    public CommonResponse<Object> killWarning(@ApiParam("报警设备号") @RequestParam("imei") String imei,
                                              @ApiParam("此次任务的唯一识别码")
                                              @RequestParam("taskIdentificationCode") String taskIdentificationCode) {
        warnStrategyService.killWarning(imei, taskIdentificationCode);
        return CommonResponse.success(null, "本次报警暂停成功");
    }

    /**
     * 本地消息
     *
     * @param
     * @return
     */
    @PostMapping("localMessage")
    @ApiOperation(value = "本地消息")
    public CommonResponse<Map<String, List<LocalMessageResponse>>> localMessage(HttpServletRequest request) {
        return CommonResponse.success(warnStrategyService.localMessage());
    }

    /**
     * 员工列表
     * @param
     * @return
     */
    @PostMapping("staffList")
    @ApiOperation(value = "员工列表")
    public CommonResponse<PageUtil<StaffListResponse>> staffList(@RequestBody @Valid StaffListRequest request) {
        return CommonResponse.success(warnStrategyService.staffList(request));
    }


    /**
     * 部门列表
     * @param
     * @return
     */
    @GetMapping("departmentList")
    @ApiOperation(value = "部门列表")
    public CommonResponse<Object> departmentList() {
        return CommonResponse.success(warnStrategyService.departmentList());
    }


}
